#include "balljoystick.h"
#include "ui_balljoystick.h"
#include <QPainter>
#include <QString>

BallJoyStick::BallJoyStick(int width,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BallJoyStick)
{
    ui->setupUi(this);
    resize(width,width);
    zeroX=width/2;
    zeroY=width/2;
    currentX=zeroX;
    currentY=zeroY;
    validRadius=width/2-30;
    mouseRadius=50;
    setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    hide();
}

BallJoyStick::~BallJoyStick()
{
    delete ui;
}

double BallJoyStick::getX()
{
    double tmpX=(currentX-zeroX)/validRadius;
    if(1<tmpX)
    {
        tmpX=1;
    }
    else if(-1>tmpX)
    {
        tmpX=-1;
    }

    return tmpX;
}

double BallJoyStick::getY()
{
    double tmpY=(currentY-zeroY)/validRadius;
    if(1<tmpY)
    {
        tmpY=1;
    }
    else if(-1>tmpY)
    {
        tmpY=-1;
    }
    return tmpY;
}

void BallJoyStick::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::gray,2));//设置画笔形式
    painter.drawLine(zeroX-validRadius,zeroY,zeroX+validRadius,zeroY);//画X直线
    painter.drawLine(zeroX,zeroY-validRadius,zeroX,zeroY+validRadius);//画Y直线
    painter.drawEllipse(zeroX-validRadius,zeroY-validRadius,validRadius*2,validRadius*2);//画参考圆
    painter.setPen(QPen(Qt::green,4));//设置画笔形式
    painter.setBrush(QBrush(Qt::green,Qt::SolidPattern)); //设置画刷形式
    painter.drawEllipse(currentX-mouseRadius,currentY-mouseRadius,mouseRadius*2,mouseRadius*2);//画当前圆
//    painter.drawRect(20,20,200,200);//画矩形
}

//--鼠标按下事件
void BallJoyStick::mousePressEvent(QMouseEvent *e)
{
    //----Qt5解决中文乱码
//    QTextCodec *codec = QTextCodec::codecForName("GB18030");

    //----QMouseEvent类提供的x()和y()可获取鼠标相对窗口的位置
    //QString str = "("+QString :: number(e->x()) +", "+QString::number(e->y())+")";

    //---点击左键
    if (Qt ::LeftButton == e->button())
    {
        currentX=e->x();
        currentY=e->y();
       // statusBar()->showMessage(codec->toUnicode("左键:") + str);
    }
    //---点击左键
    if (Qt ::RightButton == e->button())
    {
       // statusBar()->showMessage(codec->toUnicode("右键:") + str);
    }
    //---点击左键
    if (Qt ::MidButton == e->button())
    {
       // statusBar()->showMessage(codec->toUnicode("中键:") + str);
    }
    showText();
    update();
}

//---鼠标释放（松开）事件
void BallJoyStick::mouseReleaseEvent(QMouseEvent *e)
{
    //----Qt5解决中文乱码
//    QTextCodec *codec = QTextCodec::codecForName("GB18030");

    //----QMouseEvent类提供的x()和y()可获取鼠标相对窗口的位置
   // QString str = "("+QString :: number(e->x()) +", "+QString::number(e->y())+")";
   // statusBar()->showMessage(codec->toUnicode("鼠标位置:") + str, 3000);

    currentX=zeroX;
    currentY=zeroY;
    showText();
    update();
}

//--s鼠标双击事件
void BallJoyStick::mouseDoubleClickEvent(QMouseEvent *e)
{
    //---没有实现功能
}



//--鼠标移动事件
void BallJoyStick::mouseMoveEvent(QMouseEvent *e)
{
   // mousePointLabel->setText("("+QString::number(e->x())+", "+QString :: number(e->y())+")");
    currentX=e->x();
    currentY=e->y();
    showText();
    update();
}


int BallJoyStick::showText()
{
    QString tmpString;
    tmpString=tmpString+"X="+QString::number(getX(),'f',2)+" Y="+QString::number(getY(),'f',2);
    ui->label->move(zeroX,zeroY);
    ui->label->setText(tmpString);
}
